
package com.jf.cloud.delivery.controller.admin;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.delivery.dto.TransportDTO;
import com.jf.cloud.delivery.service.TransportService;
import com.jf.cloud.delivery.vo.TransportVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 运费模板
 *
 * @author zz
 * @date 2020-12-07 15:10:01
 */
@RestController("adminTransportController")
@RequestMapping("/mp/transport")
@Tag(name = "运费模板")
public class TransportController {

    @Autowired
    private TransportService transportService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取运费模板列表" , description = "分页获取运费模板列表")
    public ServerResponseEntity<PageVO<TransportVO>> page(@Valid PageDTO pageDTO,@ParameterObject TransportDTO transportDTO) {
        Long tenantId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        transportDTO.setShopId(tenantId);
        transportDTO.setSysType(sysType);

        PageVO<TransportVO> transports = transportService.page(pageDTO, transportDTO);
        return ServerResponseEntity.success(transports);
    }

    @GetMapping
    @Operation(summary = "获取运费模板" , description = "根据transportId获取运费模板")
    public ServerResponseEntity<TransportVO> getByTransportId(@RequestParam Long transportId) {
        TransportVO transportVo = transportService.getTransportAndAllItemsById(transportId);
        return ServerResponseEntity.success(transportVo);
    }

    @PostMapping
    @Operation(summary = "保存运费模板" , description = "保存运费模板")
    public ServerResponseEntity<Void> save(@Valid @RequestBody TransportDTO transportDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        transportDTO.setShopId(shopId);
        transportDTO.setSysType(sysType);
        if(Objects.isNull(transportDTO.getTransFees()) && transportDTO.getIsFreeFee() == 0){
            throw new LuckException("请选择可配送区域");
        }
        transportService.insertTransportAndTransFee(transportDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新运费模板" , description = "更新运费模板")
    public ServerResponseEntity<Void> update(@Valid @RequestBody TransportDTO transportDTO) {
        if(Objects.isNull(transportDTO.getTransFees()) && transportDTO.getIsFreeFee() == 0){
            throw new LuckException("请选择可配送区域");
        }
        transportService.updateTransportAndTransFee(transportDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "根据id批量删除运费模板" , description = "根据运费模板id批量删除运费模板")
    public ServerResponseEntity<Void> delete(@RequestParam Long[] transportIds) {
        // 删除运费模板的缓存
        for (Long transportId : transportIds) {
            transportService.deleteTransportAndTransFeeAndTransCityById(transportId);
            transportService.removeTransportAndAllItemsCache(transportId);
        }
        return ServerResponseEntity.success();
    }

    /**
     * 获取运费模板列表
     */
    @GetMapping("/list")
    public ServerResponseEntity<List<TransportVO>> list(@RequestParam(value = "shopId", required = false) Long shopId) {
        Long tenantId = AuthUserContext.get().getTenantId();
        if (Objects.equals(tenantId, Constant.PLATFORM_SHOP_ID) && Objects.nonNull(shopId)) {
            tenantId = shopId;
        }

        List<TransportVO> transports = transportService.listTransport(tenantId);
        return ServerResponseEntity.success(transports);
    }
}
